package org.jeecg.modules.InveotoryCenter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.InveotoryCenter.entity.chayiEntity;
import org.jeecg.modules.InveotoryCenter.mapper.ChayichuliMapper;
import org.jeecg.modules.InveotoryCenter.mapper.InveotoryQueryMapper;
import org.jeecg.modules.InveotoryCenter.service.ChayichuliService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChayichuliServiceImpl implements ChayichuliService {


    @Autowired
    InveotoryQueryMapper inveotoryQueryMapper;

    @Autowired
    ChayichuliMapper mapper;

    @Override
    public IPage<chayiEntity> getchayiList(Page<chayiEntity> page, String code, String differentTypeId) {
        return mapper.getchayiList(page,code,differentTypeId);
    }

    /**
     * 查询所有差异类型 返回list
     * @return
     */
    @Override
    public List<Map<String, String>> getgetAlldifferentType() {
        return mapper.getgetAlldifferentType();
    }

    /**
     * 新增差异处理
     * @param entity
     * @return
     */
    @Override
    public int addchayichuli(chayiEntity entity) {

        entity.setChayiid(System.currentTimeMillis());
        entity.setCode(String.valueOf(System.currentTimeMillis()));
        entity.setCreateTime(new Date());

        Double chayimoney=entity.getDealNumber()*Double.parseDouble(entity.getIntoUnit());
       entity.setDifferencesMoney(String.valueOf(chayimoney));
        mapper.addchayichuli(entity);
        //新增完差异单之后，往这个差异单下插入差异项目
        return  mapper.addchayiproject(entity);
    }

    /**
     * 查询差异详情
     * @param id
     * @return
     */
    @Override
    public chayiEntity getchayidetail(String id) {
        chayiEntity entity=mapper.getchayidetail( id);
        List<chayiEntity> projectlist=mapper.getchayiprojectlistbychayiId(id);
        if(projectlist!=null){

            for(int i=0;i<projectlist.size();i++){
                if(projectlist.get(i).getIsFabric()==0){
                    System.out.println("???"+   projectlist.get(i).toString());
                    HashMap<String,String> map=inveotoryQueryMapper.getGoodsdetailbyId(projectlist.get(i).getGoodsId());

                    projectlist.get(i).setGoodsCode(map.get("goods_code"));
                    projectlist.get(i).setSupplierGoodsCode(map.get("supplier_goods_code"));

                }else {

                    HashMap<String,String> map=inveotoryQueryMapper.getFabricdetailbyId( Integer.parseInt(projectlist.get(i).getGoodsId())  );
                    projectlist.get(i).setGoodsCode(map.get("fabric_code"));
                    projectlist.get(i).setSupplierGoodsCode(map.get("gysfabriccode"));

                }
            }
        }
        entity.setProjectlist(projectlist);
        return entity;
    }

    /**
     * 同意差异处理，将每个批次下的库存减一下
     * @param dealNumber
     * @param remainingInventoryId
     * @param id
     * @return
     */
    @Override
    public int applychayichuli(String dealNumber, String remainingInventoryId, String id) {
        //查询这个批次老的库存
        String  oldkucun=mapper.getpicioldkucun(remainingInventoryId);
        //然后修改这个批次的库存
        mapper.updateshengyupicikucun(remainingInventoryId,String.valueOf(Double.parseDouble(oldkucun)+Double.parseDouble(dealNumber)));

        //查询这个批次所在的的总剩余库存
        String oldSumkucun=mapper.getoldSumkucun(remainingInventoryId);

        mapper.updateSumkucun(remainingInventoryId,String.valueOf(Double.parseDouble(oldSumkucun)+Double.parseDouble(dealNumber)));
      //修改差异的状态
        return mapper.updatechayistatus(id);
    }

    /**
     * 新增差异类型
     * @param entity
     * @return
     */
    @Override
    public int adddifferenType(chayiEntity entity) {

        entity.setCreateTime(new Date());
        entity.setId(String.valueOf(System.currentTimeMillis()));
        return mapper.adddifferenType(entity);
    }
}
